#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

//节点
typedef struct QNode
{
    int data;
    struct QNode* next;
}QNode;

//创建队列(头节点)
QNode* initQueue()
{
    QNode* p=(QNode*)malloc(sizeof(QNode));
    p->next=NULL;
    p->data=0;
    return p;
}

/*
入队：
新入队的节点加在原尾节点后面
*/
QNode* enQuene(QNode* prear,int data)
{
    printf("en1:%x %d\n",prear,data);
    //1 创建节点
    QNode* p=(QNode*)malloc(sizeof(QNode));
    p->data=data;
    p->next=NULL;
    printf("en2:%x %d %x\n",p,p->data,p->next);
    //2 添加（尾插法）
    printf("en3:%x %x\n",prear,prear->next);
    prear->next=p;
    printf("en4:%x %x\n",prear,prear->next);
    prear=p;
    printf("en5:%x %x\n",prear,prear->next);
    //3 返回
    return prear;
}

/*
出队
将原来链表最前面的元素出队
*/
QNode* deQueue(QNode* top,QNode* rear)
{
    printf("de1:%x %x\n",top,rear);
    if(top->next==NULL)
    {
        printf("duilie is null\n");
        return rear; 
    }

    //1 创建临时指针
    QNode* p=top->next;
    printf("de2:%x %x\n",top->next,p);
    //2 显示数据
    printf("now chudui data:%d\n",p->data);
    //3 改指向
    printf("de3:%x %x\n",top->next,p->next);
    top->next=p->next;//p->next可能是NULL
    printf("de4:%x %x\n",top->next,p->next);
    //4 判断
    if(rear==p)
    {
        printf("de6:%x %x\n",top,rear);
        rear=top;
        printf("de7:%x %x\n",top,rear);
    }
    free(p);
    return rear;
}




int main()
{
    //注意：有头节点和首元节点
    //1 头，尾
    QNode* phead=NULL;
    QNode* prear=NULL;
    phead=prear=initQueue();

    //入队
    prear=enQuene(prear,1);
    prear=enQuene(prear,2);
    prear=enQuene(prear,3);
    prear=enQuene(prear,4);
    prear=enQuene(prear,5);


    printf("main %x %x\n",phead,prear);
    //出队
    prear=deQueue(phead,prear);
    prear=deQueue(phead,prear);
    prear=deQueue(phead,prear);
    prear=deQueue(phead,prear);
    prear=deQueue(phead,prear);
    prear=deQueue(phead,prear);

	//system("pause");
	return 0;
} 